package cn.jdy.plugins.wap.util;

import java.math.BigDecimal;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.SortedMap;

import cn.jdy.common.util.MD5Util;
import cn.jdy.framework.log.BaseLog;



/**
 * @title  在线支付公共方法
 * @author ningqian
 * @date   2017-04-13
 */

public class RedPacketUtil {
	
	/**
 	 * @Description使用系統日期生成流水单号 
 	 * 规则：年月日+时分秒+毫秒+15位随机数 //例：201608031225091565006A    
 	 * @return
 	 */
 	public static String createOrderNo(){
 		String serialnumber="";
 		try {
 			Date date = new Date();
 			SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
 			serialnumber = sdf.format(date);
 			//serialnumber+=generateString(15);
 			
 		}catch (Exception e){
 			e.printStackTrace();
 		}
 		return serialnumber;
 	}
	 
	/**
	 * @Description 生成微信统一下单 支付随机数
	 * @return
	 */
    public static String pay_nonce_str() {
   	 Date now = new Date();  
        SimpleDateFormat outFormat = new SimpleDateFormat("yyyyMMddHHmmss");  
   	 String currTime = outFormat.format(now);  
   	 String strTime = currTime.substring(8, currTime.length());
   	 String strRandom = buildRandom(4) + "";
   	 // 随机字符串
   	 String nonce_str = strTime + strRandom;
   	 return nonce_str;
    }
    
    
    public static int buildRandom(int length) {  
        int num = 1;  
        double random = Math.random();  
        if (random < 0.1) {  
            random = random + 0.1;  
        }  
        for (int i = 0; i < length; i++) {  
            num = num * 10;  
        }  
        return (int) ((random * num));  
    }  
    
    
    /**
     * @Description 将红包金额总额转换为整数（以分为单位） 
     * @param total 红包总额
     * @return
     */
    public static String get_total_fee(double total) { 
	      int total_fee = (int)(total*100);
//	      System.err.println(total_fee);
	      return String.valueOf(total_fee);
    }
    
    
    /** 
     * @Description：sign签名 
     * @param characterEncoding 编码格式 
     * @param parameters 请求参数 
     * @return 
     */  
    public static String createSign(String characterEncoding, SortedMap<Object, Object> packageParams, String API_KEY) {  
        StringBuffer sb = new StringBuffer();  
        Set es = packageParams.entrySet();  
        Iterator it = es.iterator();  
        while (it.hasNext()) {  
            Map.Entry entry = (Map.Entry) it.next();  
            String k = (String) entry.getKey();  
            String v = (String) entry.getValue();  
            if (null != v && !"".equals(v) && !"sign".equals(k) && !"key".equals(k)) {  
                sb.append(k + "=" + v + "&");  
            }  
        }  
        sb.append("key=" + API_KEY);  
        String sign = MD5Util.MD5Encode(sb.toString(), characterEncoding).toUpperCase();  
        return sign;  
    }  
    
 
    /** 
     * @Description：将请求参数转换为xml格式的string 
     * @param parameters  请求参数 
     * @return 
     */  
    public static String getRequestXml(SortedMap<Object, Object> parameters) {  
        StringBuffer sb = new StringBuffer();  
        sb.append("<xml>");  
        Set es = parameters.entrySet();  
        Iterator it = es.iterator();  
        while (it.hasNext()) {  
            Map.Entry entry = (Map.Entry) it.next();  
            String k = (String) entry.getKey();  
            String v = (String) entry.getValue();  
            if ("attach".equalsIgnoreCase(k) || "body".equalsIgnoreCase(k) || "sign".equalsIgnoreCase(k)) {  
                sb.append("<" + k + ">" + "<![CDATA[" + v + "]]></" + k + ">");  
            } else {  
                sb.append("<" + k + ">" + v + "</" + k + ">");  
            }  
        }  
        sb.append("</xml>");  
        return sb.toString();  
    }  
    
    /** 
     * 提供精确的加法运算。 
     * @param value1 被加数 
     * @param value2 加数 
     * @return 两个参数的和 
     */  
    public static Double add(Number value1, Number value2) {  
        BigDecimal b1 = new BigDecimal(Double.toString(value1.doubleValue()));  
        BigDecimal b2 = new BigDecimal(Double.toString(value2.doubleValue()));  
        return b1.add(b2).doubleValue();  
    }  
  
    /** 
     * 提供精确的减法运算。 
     *  
     * @param value1 
     *            被减数 
     * @param value2 
     *            减数 
     * @return 两个参数的差 
     */  
    public static double sub(Number value1, Number value2) {  
        BigDecimal b1 = new BigDecimal(Double.toString(value1.doubleValue()));  
        BigDecimal b2 = new BigDecimal(Double.toString(value2.doubleValue()));  
        return b1.subtract(b2).doubleValue();  
    }  
    
    
    /**
     * 获取当前ip地址
     * @return
     */
    public static String getHostAddress(){
    	String hostAddress = "192.168.1.1";
    	try {
			InetAddress res = InetAddress.getLocalHost();
			hostAddress = res.getHostAddress();
		} catch (UnknownHostException e) {
			BaseLog.e(RedPacketUtil.class, "获取本机ip失败\t"+e.getMessage(), e);
		}
    	return hostAddress;
    }
    
    public static void main(String[] args) {
		String s = getHostAddress();
	}
}
